import cn.hutool.crypto.digest.MD5;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import util.DBUtil;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TcpSocketRegistServer {

    public static void main(String[] args) throws IOException,SQLException {
        TcpSocketRegistServer server = new TcpSocketRegistServer(9090);
        server.start();
    }
    // 1. 创建 serverSocket
    private ServerSocket serverSocket = null;

    public TcpSocketRegistServer(int port) throws IOException {
        // 2. 根据端口号进入具体服务
        serverSocket = new ServerSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务器启动！");
        // 3. 定义线程池
        ExecutorService pool = Executors.newCachedThreadPool();
        while (true) {
            // 4. 调用 assept 建立短连接
            Socket clientSocket = serverSocket.accept();
            // 5. 从线程池获取一个线程来执行任务
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        processRegistConnection(clientSocket);
                    }  catch (IOException | SQLException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    /**
     * 处理录入身份信息功能任务
     * @param socket 连接信息
     */
    @SuppressFBWarnings({"OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE", "DM_DEFAULT_ENCODING", "DM_DEFAULT_ENCODING", "VA_FORMAT_STRING_USES_NEWLINE"})
    private void processRegistConnection(Socket socket) throws IOException, SQLException {
        System.out.println("录入身份信息功能启动......");
        System.out.printf("[%s:%d] 客户端建立连接!\n", socket.getInetAddress().toString(), socket.getPort());
        // 获取输入/输出流信息
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        Scanner scanner = new Scanner(br);
        // 5.1 循环处理每个请求，分别返回响应
        while (true) {
            // 5.2 接收客户端数据
            String json = scanner.next(); // 读一行数据
            String[] infos = json.split(",");
            String username = infos[0];
            String password = infos[1];
            if (!"".equals(username) || !"".equals(password)) {
                if(username.length() <= 7 && password.length() <= 10) {
                    // 5.3 操作数据库
                    // 5.3.1 得到数据库连接 connection
                    Connection connection = DBUtil.getConnection();
                    String sql = "insert into user(`username`,`password`) values(?,?)";
                    PreparedStatement statement = connection.prepareStatement(sql);
                    // 5.3.2 填充占位符
                    statement.setString(1, username);
                    statement.setString(2, MD5.create().digestHex(password));
                    // 5.3.3 执行 SQL
                    int result = statement.executeUpdate();// 返回一个受影响的行数
                    System.out.println("受影响的行数： " + result);
                    // 5.4 关闭资源
                    DBUtil.close(statement, connection);
                    bw.write("录入身份信息成功......");
                    System.out.println("录入身份信息成功......\n");
                } else {
                    System.err.println("参数错误\n");
                    bw.write("录入身份信息失败......");
                }
            } else {
                System.err.println("参数错误\n");
                bw.write("录入身份信息失败......");
            }
            // 6 关闭并刷新
            bw.newLine();
            bw.flush();
            bw.close();
            socket.close();
        }
    }
}
